package com.android.SchoolManagerSystem.action;

import java.net.HttpURLConnection;
import java.util.HashMap;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.android.SchoolManagerSystem.CommonInfo;
import com.android.SchoolManagerSystem.data.Curricula;
import com.android.SchoolManagerSystem.data.Userinfo;
import com.android.SchoolManagerSystem.net.HttpRequester;
import com.android.SchoolManagerSystem.net.HttpResponser;

public class UploadRecord {
		private Userinfo 	curinfo = null;
		private Curricula 	Cculatmp = null;
		private boolean 	berrorcode = false;
		public final static int OPER_WEEKLYREPORT = 0;
		public final static int OPER_PRITICEREPORT = OPER_WEEKLYREPORT + 1;

		public UploadRecord(Userinfo uinfo) {
			curinfo = uinfo;
		}

		public boolean go(int arg) {
			// decodelogresponce(null);
			// return false;

			
			String strHosturi = CommonInfo.strGetTranscripturi;
			String strrequetstXMl = generatexmlstring(arg);
			
			switch(arg)
			{
				case OPER_WEEKLYREPORT:
					strHosturi = CommonInfo.strGetTranscripturi;
					break;
				case OPER_PRITICEREPORT:
					strHosturi = CommonInfo.strGetTranscripturi;
					break;
				default:
					strHosturi = CommonInfo.strGetTranscripturi;
						break;
			}
			
			HashMap<String, String> propertys = new HashMap<String, String>();
			propertys.put("Set-Cookie", "ONE_VER=1_0;sid=10006");
			propertys.put("H-Product", "client");
			HttpRequester loginrequest = new HttpRequester();
			HttpResponser loginresponse = loginrequest.sendPost(
					strHosturi, propertys, strrequetstXMl);
			if (loginresponse == null
					|| loginresponse.getCode() != HttpURLConnection.HTTP_OK)
				return false;
			else {
				if (Decodesvrreponse(arg,loginresponse))
					return true;
				else
					return false;
			}
		}
		/**
		 * Send http request, and return http response
		 * 
		 * @author jeson_li
		 * @date 2011-3-28
		 */
		private boolean Decodesvrreponse(int arg,HttpResponser response )
		{
			boolean bres = false;
			switch(arg)
			{
				case OPER_WEEKLYREPORT:
					decodeUpdateRes(response);
					break;
				case OPER_PRITICEREPORT:
				default:
					decodeUpdateRes(response);
					break;
			}
			
			return bres;
		}
		
		
		private String generatexmlstring(int arg) {
			String strlogreq = null;
			switch(arg)
			{
				case OPER_WEEKLYREPORT:
				default:
					 strlogreq = "123";
					break;
				case OPER_PRITICEREPORT:
					strlogreq = "123";
					break;
			}		

			return strlogreq;
		}
		
		private boolean decodeUpdateRes(HttpResponser repsonse) {
			//String testxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><queryallscorer apiversion=\"1.0\"><ccula><culaid>math</culaid><credits>2</credits><mark>88</mark></ccula><ccula><culaid>english</culaid><credits>3</credits><mark>80</mark></ccula></queryallscorer>";
			RootElement root = new RootElement("queryallscorer");

			root.setStartElementListener(new StartElementListener() {
				@Override
				public void start(Attributes attributes) {
				}
			});
			root.setEndElementListener(new EndElementListener() {
				@Override
				public void end() {
				}
			});

			root.getChild("errorcode").setEndTextElementListener(
					new EndTextElementListener() {
						@Override
						public void end(String body) {
							if(body == "0")
								berrorcode = false;
							else
								berrorcode = true;
						}
					});

			try {
				Xml.parse(repsonse.getContent(), root.getContentHandler());
			} catch (Exception e1) {
				e1.printStackTrace();
				return false;
			}
			if (berrorcode)
				return false;
			else
				return true;
		}
}

